import http from '../axios'
import { Dayjs } from 'dayjs'

interface UserType {
  username: string
  email: string
  password: string
}

interface LoginType {
  token: string
  userInfo: UserInfoType
}

interface UserInfoType {
  username: string
  birthdate?: string | null
  bio?: string | null
  avatar?: string | null
  sex?: string | null
}

interface UpdateType {
  username?: string
  birthdate?: string | null | Dayjs
  bio?: string | null
  avatar?: string | null
  sex?: string | null
}

type LoginParams = Pick<UserType, 'email' | 'password'>
// 注册
export const createUser = (params: UserType) => {
  return http.post('/user/createUser', params)
}

// 登录
export const login = (params: LoginParams) => {
  return http.post<LoginType>('/user/login', params)
}

// 修改用户信息
export const updateUserInfo = (params: UpdateType) => {
  return http.post('/user/updateUserInfo', params)
}

// 获取用户信息
export const getUserInfo = () => {
  return http.get<UserInfoType>('/user/getUserInfo')
}

// 上传头像
export const uploadAvatar = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return http.post('/user/upload/avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  })
}
